﻿using System;

namespace EasyCommonCore.Extension
{
    /// <summary>
    /// 抛出错误异常
    /// </summary>
    public class AssertExtend
    {
        #region 判断为空

        #region 判断属性为空（非class类型）
        /// <summary>
        /// 为空错误
        /// </summary>
        /// <param name="name">参数</param>
        /// <param name="msg">提示</param>
        public static void IsNotNull(object name, string msg)
        {
            if (name == null)
            {
                throw new ArgumentException($"{msg}不能为空");
            }
            if (name is string)
                IsNotNull(name.ToString(), msg);
            if (name is DateTime)
                IsNotNull((DateTime)name, msg);
        }

        /// <summary>
        /// 为空错误
        /// </summary>
        /// <param name="name">参数</param>
        /// <param name="msg">提示</param>
        private static void IsNotNull(string name, string msg)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException($"{msg}不能为空");
            }
        }

        /// <summary>
        /// 为空错误
        /// </summary>
        /// <param name="name">参数</param>
        /// <param name="msg">提示</param>
        private static void IsNotNull(int name, string msg)
        {
            if (name == default(int))
            {
                throw new ArgumentException($"{msg}不能为空");
            }
        }

        /// <summary>
        /// 为空错误
        /// </summary>
        /// <param name="name">参数</param>
        /// <param name="msg">提示</param>
        private static void IsNotNull(DateTime name, string msg)
        {
            if (name == default(DateTime))
            {
                throw new ArgumentException($"{msg}不能为空");
            }
        }

        #endregion

        #region 判断Class为空
        /// <summary>
        /// 判断Class为空
        /// </summary>
        /// <param name="name">实体信息</param>
        /// <param name="msg">自定义信息</param>
        public static void IsNotNullByClass(object name, string msg)
        {
            if (name == null)
                throw new ArgumentException(msg);
        }

        #endregion

        #endregion



        #region 两个字符串不相等
        /// <summary>
        /// 两个字符串不相等
        /// </summary>
        /// <param name="id1">字符串1</param>
        /// <param name="id2">字符串2</param>
        /// <param name="errorMessageFormat">错误内容</param>
        public static void AreEqual(string id1, string id2, string errorMessageFormat)
        {
            if (id1 != id2)
            {
                throw new ArgumentException(string.Format(errorMessageFormat, id1, id2));
            }
        }
        #endregion
    }
}
